/*弹出层登录框
 @企业登录按钮  -> 用法
 * @csrf:       令牌
 * @callBack    响应返回的状态
 $(".companylogin").click(function(){
 $.companyEject({csrf:"{{ csrf_token() }}",callBack:function(status){
 alert(status);//422 密码错误，0成功，但目前做了 reload
 }});
 });
 @个人登录按钮  -> 用法
 * @手机登录需在模版里加载验证和路径
 $(".personlogin").click(function(){
 $.personEject({csrf:"{{ csrf_token() }}",callBack:function(status){
 alert(status);//422 账号密码验证码错误，0登录成功，2表示手机号空或格式不对,3表示验证码少于六位
 }});
 });
 */
$(function () {
    $.extend({
        companyEject: function (settings) {
            var setvar = {
                className: "companylogin",
                csrf: "",
                QQLogin: "",
                weiXin: "",
                weiXinUrl: "",
                callBack: function () {
                }
            };
            $.extend(setvar, settings);
            //弹出层登录
            //$("."+setvar.className).click(function(){
            //内容显示区
            var loginhtml = '<div class="content">' +
                '<form class="left"  id="companyForm" action="#" method="post"  name="companyForm">' +
                '<input type="hidden" name="_token" value="' + setvar.csrf + '">' +
                '<input id="username" name="username" type="text" placeholder="请输入用户名">' +
                '<input id="password" name="password" type="password" placeholder="请输入密码">' +
                '<div class="box">' +
                '<input id="remember" type="checkbox">' +
                '<label for="remember">记住密码</label>' +
                '<a href="/company/find_password">忘记密码？</a>' +
                '</div>' +
                '<input class="sub" type="submit" value="登录">' +
                '<div class="box reg">' +
                '<span>还不是企业会员？</span>' +
                '<a href="/company/register">立即注册</a>' +
                '</div>' +
                '</form>' +
                '<div class="right">' +
                '<div class="wxCode">' +
                '<img src="/new/images/logo/code_html5.png" alt="">' +
                '<span>[扫一扫使用微信端]</span>' +
                '</div>' +
                '<div class="othersLogin">' +
                '<span>使用以下账号直接登录:</span>' +
                '<div class="box">' +
                '<a class="qq" href="company/qqlogin"></a>' +
                '<a class="wx" href="' + setvar.weiXinUrl + '"></a>' +
                '</div>' +
                '</div>' +
                '</div>' +
                '</div>';
            companyLogin = layer.open({
                type: 1,
                title: "<b style='color:#3498db'>企业登录</b>",
                skin: 'layer-company-login',
                area: ['742px', '415px'],
                resize: false,
                move: false,
                btn: false,
                content: loginhtml
            });
            //企业登录
            $(".sub").click(function () {
                var $compForm = $("#companyForm");
                var username = str_trim($compForm.find('#username').val());
                if (username == '') {
                    layer.msg("用户名不能为空!", {icon: 5, time: 2000});
                    $compForm.find('#username').focus();
                    $compForm.find('#username').val(username);
                    return false;
                }
                if ($compForm.find('#password').val() == '' || $compForm.find('#password').val().length < 6) {
                    layer.msg("密码不能为空,或者密码长度不能少于6位!", {icon: 5, time: 2000});
                    $compForm.find('#password').focus();
                    return false;
                }
                var queryString = $compForm.formSerialize();
                $.ajax({
                    url: '/company/ajax_company_login',
                    dataType: "json",
                    data: queryString,
                    type: "POST",
                    success: function (data) {
                        if (data.status == '0') {
                            layer.msg(data.msg, {icon: 6});
                            setvar.callBack(data.status);
                            window.location.reload();
                        } else {
                            layer.msg(data.msg, {icon: 6});
                            setvar.callBack(data.status);
                        }
                    },
                    error: function (data) {
                        var msgErr = '';
                        var errors = $.parseJSON(data.responseText);
                        $.each(errors, function (index, value) {
                            msgErr += value + ";<br>";
                        });
                        layer.msg(msgErr, {icon: 5});
                        setvar.callBack(data.status);
                    },
                    complete: function () {
                    }
                });
                return false;
            });
            // });
        }
    })
});//end

//个人登录框
$(function () {
    $.extend({
        personEject: function (settings) {
            var setvar = {
                className: "personlogin",
                QQLogin: "",
                QQUrl: "",
                weiXin: "",
                weiXinUrl: "",
                csrf: "",
                callBack: function () {
                }
            };
            $.extend(setvar, settings);
            //弹出层登录
            //$("."+setvar.className).click(function(){

            var loginhtml = '<div class="content">' +
                '<div class="left">' +
                '<ul class="switch">' +
                '<li class="active0"><i class="icon1"></i><span>普通方式登录</span></li>' +
                '<li><i class="icon2"></i><span>手机动态码登录</span></li>' +
                '</ul>' +
                '<form class="commonLog" id="personalForm" name="personalForm" action="#" method="post">' +
                '<input type="hidden" name="_token" value="' + setvar.csrf + '">' +
                '<input id="username" name="username" type="text" placeholder="请输入用户名">' +
                '<input id="password" name="password" type="password" placeholder="请输入密码">' +
                '<div class="box">' +
                '<input id="remember" type="checkbox"><label for="remember">记住密码</label>' +
                '<a href="/company/find_password">忘记密码？</a></div>' +
                '<input id="personsubmit" type="submit" value="登录">' +
                '<div class="box reg"><span>还不是企业会员？</span><a href="/personal/register">立即注册</a></div>' +
                '</form>' +
                '<form class="phoneCode hide" method="post">';
            var csrf_field_value = ($("." + setvar.className).next('input').val());  //csrf 值
            if (csrf_field_value == '' || csrf_field_value == undefined) {
                csrf_field_value = setvar.csrf;
            }
            loginhtml += '<input type="hidden" name="_token" value="' + csrf_field_value + '">' +
                '<input id="mobile" name="mobile" type="text"  placeholder="请输入手机号码">' +
                '<input id="mobCode" name="mobCode" type="text" placeholder="请输入手机验证码">' +
                '<input id="getMobCode" name="mobcode" type="button" value="点击获取验证码">' +
                '<input id="mobileSubmit" type="submit" value="登录">' +
                '<div class="box reg"><span>还不是个人会员？</span><a href="/personal/register">立即注册</a></div>' +
                '</form>' +
                '</div>' +
                '<div class="right">' +
                '<div class="wxCode">' +
                '<img src="/new/images/logo/code_html5.png" alt="">' +
                '<span>[扫一扫使用微信端]</span>' +
                '</div>' +
                '<div class="othersLogin">' +
                '<span>使用以下账号直接登录:</span>' +
                '<div class="box">' +
                '<a class="qq" href="company/qqlogin"></a><a class="wx" href=""></a>' +
                '</div>' +
                '</div>' +
                '</div>' +
                '</div>';

            personLogin = layer.open({
                type: 1,
                title: "<b style='color:#3498db'>个人登录</b>",
                skin: 'layer-personal-login',
                area: ['750px', '420px'],
                resize: false,
                move: false,
                btn: false,
                content: loginhtml
            });

            //普通登录、手机验证码登录切换
            $('.layer-personal-login .switch li').click(function () {
                var index = $(this).index();
                if (index == 0) {
                    $(this).addClass('active0').siblings().removeClass('active1');
                    $('.layer-personal-login .left form').addClass('hide').eq(index).removeClass('hide');
                } else {
                    $(this).addClass('active1').siblings().removeClass('active0');
                    $('.layer-personal-login .left form').addClass('hide').eq(index).removeClass('hide');
                }
            });

            //获取验证码按钮点击事件
            $('#getMobCode').click(function () {
                getModCode($(this), $('#mobile'));
            });

            //检查个人用户登录
            $("#personsubmit").click(function () {
                oForm = "#personalForm";
                var username = str_trim($(oForm).find('#username').val());
                if (username == '') {
                    layer.msg("用户名/手机号码不能为空!", {icon: 5, time: 2000});
                    $(oForm).find('#username').focus();
                    $(oForm).find('#username').val(username);
                    return false;
                }
                if ($(oForm).find('#password').val() == '' || $(oForm).find('#password').val().length < 6) {
                    layer.msg("密码不能为空,或者密码长度不能少于6位!", {icon: 5, time: 2000});
                    $(oForm).find('#password').focus();
                    return false;
                }
                var queryString = $(oForm).formSerialize();
                $.ajax({
                    url: '/personal/ajax_login',
                    dataType: "json",
                    data: queryString,
                    type: "POST",
                    success: function (data) {
                        if (data.status == '0') {
                            layer.msg(data.msg, {icon: 6});
                            setvar.callBack(data.status);
                            window.location.reload();
                            //$("#parentdiv").fadeToggle();
                        } else {
                            layer.msg(data.msg, {icon: 5});
                            setvar.callBack(data.status);
                        }
                    },
                    error: function (data) {
                        var msgErr = '';
                        var errors = $.parseJSON(data.responseText);
                        $.each(errors, function (index, value) {
                            msgErr += value + ";<br>";
                        });
                        setvar.callBack(data.status);
                        layer.msg(msgErr, {icon: 5});
                    },
                    complete: function () {
                    }
                });
                return false;
            });
            //个人手机登录
            $("#mobileSubmit").click(function () {
                //onsubmit=\"return checkPersonMobile('#personalMobileForm')\"
                oForm = "#personalMobileForm";
                var mobilenum = str_trim($(oForm).find('#mobile').val());
                if (mobilenum == '') {
                    layer.msg("手机号码不能为空!", {icon: 5, time: 2000});
                    $(oForm).find('#mobile').focus();
                    $(oForm).find('#mobile').val(mobilenum);
                    setvar.callBack(2);
                    return false;
                } else {
                    var regu = /(^1[3-9]{1}[0-9]{9}$)/;
                    if (!regu.test(mobilenum)) {
                        layer.msg("手机号码格式不正确!", {icon: 5, time: 2000});
                        setvar.callBack(2);
                        return false;
                    }
                }

                if ($(oForm).find('#mobCode').val() == '' || $(oForm).find('#mobCode').val().length != 6) {
                    layer.msg("验证码不能为空,或者验证码长度少于6位!", {icon: 5, time: 2000});
                    $(oForm).find('#mobCode').focus();
                    setvar.callBack(3);
                    return false;
                }

                var queryString = $(oForm).formSerialize();
                $.ajax({
                    url: "personal/ajax_mobile_login",
                    dataType: "json",
                    data: queryString,
                    type: "POST",
                    success: function (data) {
                        if (data.status == '0') {
                            layer.msg(data.msg, {icon: 6});
                            setvar.callBack(data.status);
                            window.location.reload();
                        } else {
                            layer.msg(data.msg, {icon: 5});
                            setvar.callBack(data.status);
                        }
                    },
                    error: function (data) {
                        var msgErr = '';
                        var errors = $.parseJSON(data.responseText);
                        $.each(errors, function (index, value) {
                            msgErr += value + ";<br>";
                        });
                        layer.msg(msgErr, {icon: 5});
                        setvar.callBack(data.status);
                    },
                    complete: function () {
                    }
                });
                return false;
            });


            //个人会员手机号码验证
            $.validator.addMethod("checkMobile", function (value, element) {
                var regu = /(^1[3-9]{1}[0-9]{9}$)/;
                if (regu.test(value)) {
                    return true;
                } else {
                    return false;
                }
            }, "请输入正确的手机号码");

            if (ajaxSmsUrl == undefined || _token == undefined) {
                var ajaxSmsUrl = '';
                var _token = '';
            }
            $('#personalMobileForm').validate({
                //指定使用什么标签标记错误。
                errorElement: "span",
                //一个函数，可以自定义错误放到哪里。
                errorPlacement: function (error, element) {
                    $(element).parents('dl').find(".errinfo").append(error);
                },
                //自定义错误消息
                tipPlacement: function (element, tip) {
                    $(element).parents('dl').find(".errinfo").append(tip);
                },
                //通过验证后执行的函数
                success: function (label) {
                    label.text('ok!').addClass('success');
                },
                //未验证后执行的函数
                highlight: function (element, errorClass) {
                    $(element).closest("dl").find("dd.errinfo").find('span').removeClass('success');
                },
                //js上面添加验证规则
                rules: {
                    mobile: {
                        required: true,
                        minlength: 11,
                        checkMobile: true //期望的是true,如果为false则展示提示信息
                    },
                    mobCode: {
                        required: true,
                        remote: {
                            url: ajaxSmsUrl,             //后台处理程序
                            type: "post",               //数据发送方式
                            data: {                     //要传递的数据
                                mobCode: function () {
                                    return $("#mobCode").val();
                                },
                                mobile: function () {
                                    return $("#mobile").val();
                                },
                                _token: _token
                            },
                            dataType: "html",
                            dataFilter: function (data, type) {
                                return (data == "true") ? true : false;
                            }
                        }
                    }
                },
                //错误信息提示修改
                messages: {
                    mobile: {
                        required: "请输入手机号码",
                        minlength: "手机号码长度必须是11位数字"
                    },
                    mobCode: {
                        required: "请输入验证码",
                        remote: $.validator.format('验证码错误，请重新输入')
                    }
                },
                submitHandler: function (form) {
                    var queryString = $('#loginMobile').formSerialize();
                    $.ajax({
                        url: '/personal/ajax_mobile_login',
                        dataType: "json",
                        data: queryString,
                        type: "POST",
                        beforeSend: function () {
                            layer.load();
                        },
                        success: function (data) {
                            if (data.status == '0') {
                                layer.msg(data.msg, {icon: 6});
                                location = welcomeUrl;
                            } else {
                                layer.msg(data.msg, {icon: 5});
                            }
                        },
                        error: function (data) {
                            var msgErr = '';
                            var errors = $.parseJSON(data.responseText);
                            $.each(errors, function (index, value) {
                                msgErr += value + ";<br>";
                            });
                            layer.msg(msgErr, {icon: 5});
                        },
                        complete: function () {
                            layer.closeAll('loading');
                        }
                    });
                }
            });
            //});
        }
    });

});//end